package com.anhry.www.safety.manage.oa.struts.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.www.safety.manage.oa.bean.TbOaNoticeFj;
import com.anhry.www.safety.manage.oa.struts.base.OABaseAction;
import com.anhry.www.safety.util.Util;


/**
 * 
 * @author chen
 * @date 2012-8-15
 */
@Controller
@Scope("prototype")
@SuppressWarnings("unchecked")
public class NoticeDownloadAction extends OABaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String file;//要下载生成的文件名（上传时的文件名）	
	private String fileName;//实际文件的名称
	private boolean noFile;//文件不存在
	private Long id;
	private String filePath;

	public boolean isNoFile() {
		return noFile;
	}
	public void setNoFile(boolean noFile) {
		this.noFile = noFile;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	// getFile()是处理保存中文文件名带来的问题，主要是用来处理保存文件时中文名字乱码问题
	public String getFile() {
		return this.file;
	}

	public void setFile(String file) {
		this.file = file;
	}

	// 对于上边的文件路径，给出他的输入流(实际文件资源)，对应在配置文件中的InputName属性名
	public InputStream getInputStream() {
		//System.out.println("得到实际文件资源file：" + fileName);
		//System.out.println("/"+this.getFilePath()+fileName);
		//InputStream o = ServletActionContext.getServletContext().getResourceAsStream("/"+this.getFilePath()+fileName);
		//原getResourceAsStream 的文件流获取方法 无法使用绝对路径；改为new FileInputStream(this.getFilePath()+fileName);
		InputStream o;
		try {
			//System.out.println(this.getFilePath()+fileName);
			o = new FileInputStream(Util.SAFETY_FILE + File.separator+this.getFilePath()+fileName);
			return o;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String execute() throws Exception {
		TbOaNoticeFj f = null;
		try {
			if(null!=this.getId() && !"".equals(this.getId())){
				f = (TbOaNoticeFj)this.utilService.getObjectById(TbOaNoticeFj.class, this.getId());
				if(null!=f){				
					if(null!=f.getFjPath() && !"".equals(f.getFjPath())){
						if(checkExist(f.getFjPath())){
							f.setFjPath(f.getFjPath().replaceAll("\\\\","/"));
							this.setFilePath(f.getFjPath().substring(0, f.getFjPath().lastIndexOf("/")+1));
							this.setFile(new String(f.getFjTitle().getBytes("gbk"),"iso-8859-1"));
							this.setFileName(f.getFjPath().substring(f.getFjPath().lastIndexOf("/")+1,f.getFjPath().length()));
							return "success";
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}		
		this.setNoFile(true);
		return "nofile";
	}
	/**
	 * 判断文件是否存在
	 * @param path
	 * @return
	 */
	public boolean  checkExist(String path){
		java.io.File file = new java.io.File(Util.SAFETY_FILE + File.separator+path);       
		if(file.exists()){       
			return true;    
		}else{       
			return false;       
		}       
	}

}
